package com.spark.colliery.widget

import android.content.Context
import android.os.Parcel
import android.os.Parcelable
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import com.spark.colliery.R

/**
 * Setting Item View
 */

class SettingView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = -1) : RelativeLayout(context, attrs, defStyleAttr) {

    private val tvSettingName: TextView
    private val ivSwitch: ImageView

    /**
     * 获取当前开关状态
     *
     * @return
     */
    var isSwitchOn = true
        private set
    /**
     * 设置名称
     *
     * @return
     */
    var settingName: String? = ""
    private var onSwitchChangedListener: OnSwitchChangedListener? = null

    init {

        val rootView = LayoutInflater.from(context).inflate(R.layout.custom_setting_view, this)
        tvSettingName = rootView.findViewById(R.id.tv_setting_name)
        ivSwitch = rootView.findViewById(R.id.iv_switch)

        val ta = context.obtainStyledAttributes(attrs, R.styleable.SettingView)
        settingName = ta.getString(R.styleable.SettingView_name)
        isSwitchOn = ta.getBoolean(R.styleable.SettingView_isOn, true)
        ta.recycle()

        ivSwitch.setOnClickListener {
            updateSwitch()
            if (onSwitchChangedListener != null) {
                onSwitchChangedListener!!.onSwitchChanged(isSwitchOn)
            }
        }
    }

    /**
     * 更新开关状态及图标
     */
    fun updateSwitch() {
        if (isSwitchOn) {
            ivSwitch.setImageResource(R.drawable.btn_switch_off)
        } else {
            ivSwitch.setImageResource(R.drawable.btn_switch_on)
        }
        isSwitchOn = !isSwitchOn

    }

    fun initState() {
        if (isSwitchOn) {
            ivSwitch.setImageResource(R.drawable.btn_switch_on)
        } else {
            ivSwitch.setImageResource(R.drawable.btn_switch_off)
        }
    }

    /**
     * 设置switch状态监听
     *
     * @param listener
     */
    fun setOnSwitchChangedListener(listener: OnSwitchChangedListener?) {
        if (listener == null) {
            return
        }
        onSwitchChangedListener = listener
    }

    override fun onSaveInstanceState(): Parcelable? {
        val superState = super.onSaveInstanceState()
        val ss = SavedState(superState)
        ss.settingName = this.settingName
        ss.isSwitchOn = this.isSwitchOn
        return ss
    }

    override fun onRestoreInstanceState(state: Parcelable) {
        val ss = state as SavedState
        super.onRestoreInstanceState(state)
        tvSettingName.text = ss.settingName
        this.isSwitchOn = ss.isSwitchOn
        initState()
    }

    override fun onFinishInflate() {
        super.onFinishInflate()
        tvSettingName.text = settingName
        initState()
    }


    interface OnSwitchChangedListener {
        fun onSwitchChanged(isSwitchOn: Boolean)
    }

    /**
     * save state <><><><><><><><><><><><><><><><<>>>>>>>>>>>>>>>>>
     */
    internal class SavedState : View.BaseSavedState {
        var isSwitchOn: Boolean = false
        var settingName: String? = null

        constructor(source: Parcel) : super(source) {
            isSwitchOn = source.readInt() == 0
            settingName = source.readString()
        }

        constructor(superState: Parcelable) : super(superState) {}

        override fun writeToParcel(out: Parcel, flags: Int) {
            super.writeToParcel(out, flags)
            out.writeInt(if (isSwitchOn) 0 else 1)
            out.writeString(settingName)
        }

        companion object {

            val CREATOR: Parcelable.Creator<SavedState> = object : Parcelable.Creator<SavedState> {

                override fun createFromParcel(source: Parcel): SavedState {
                    return SavedState(source)
                }

                override fun newArray(size: Int): Array<SavedState?> {
                    return arrayOfNulls(size)
                }
            }
        }

    }
}
